<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <title>生产过程管理系统 - 管理员</title>
    <link rel="stylesheet" href="../../css/style.css">
    <!-- 引入 jQuery-->
    <script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js"></script>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <!-- 引入 Axios -->
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    <style>
        .submenu {
            display: none;
        }

        .menu-item.active>.submenu {
            display: block;
        }

        .menu-arrow {
            transition: transform 0.2s;
        }

        .menu-item.active>a>.menu-arrow {
            transform: rotate(90deg);
        }
    </style>
    <script>
        // 检查用户是否已登录，以及是否有权限访问该页面
        document.addEventListener('DOMContentLoaded', function() {
            // 检查用户是否已登录
            const userInfo = localStorage.getItem('userInfo');
            if (!userInfo) {
                // 未登录，跳转到登录页面
                window.location.href = '../../login.html';
                return;
            }
            
            try {
                const user = JSON.parse(userInfo);
                // 检查用户角色是否为管理员
                if (user.role !== '管理员') {
                    alert('您没有权限访问此页面');
                    window.location.href = '../../login.html';
                    return;
                }
            } catch (e) {
                // 解析错误，清除无效的用户信息
                localStorage.removeItem('userInfo');
                window.location.href = '../../login.html';
                return;
            }

            // 初始化菜单交互
            var menuItems = document.querySelectorAll('.menu-item > a');
            menuItems.forEach(function (item) {
                item.addEventListener('click', function (e) {
                    var parent = item.parentElement;
                    // 只允许单开一个折叠区
                    document.querySelectorAll('.menu-item').forEach(function (el) {
                        if (el !== parent) el.classList.remove('active');
                    });
                    parent.classList.toggle('active');
                    e.preventDefault();
                });
            });
        });
    </script>
</head>

<body>
    <div class="admin-layout">
        <!-- 顶部导航栏 -->
        <header class="admin-header">
            <div class="header-left">
                <div class="logo">生产过程管理系统</div>
            </div>
            <div class="header-right">
                <span class="user-info">管理员</span>
                <span class="user-name">
                    <script>
                        try {
                            const userInfo = localStorage.getItem('userInfo');
                            const user = JSON.parse(userInfo);
                            document.write(user.name || '未登录');
                        } catch (e) {
                            document.write('未登录');
                        }
                    </script>
                </span>
                <button class="logout-btn" onclick="logout()">退出</button>
            </div>
        </header>

        <!-- 左侧菜单 -->
        <aside class="admin-sidebar">
            <ul class="menu-list">
                <li class="menu-item">
                    <a href="../homepage/administrator_home.html">
                        <span class="menu-icon">🏠</span>
                        <span class="menu-title">首页</span>
                    </a>
                </li>
                <li class="menu-item">
                    <a href="javascript:void(0)">
                        <span class="menu-icon">👥</span>
                        <span class="menu-title">人员管理</span>
                        <span class="menu-arrow"></span>
                    </a>
                    <ul class="submenu">
                        <li><a href="person/person_list.html">人员列表</a></li>
                        <li><a href="person/person_edit.html">添加人员</a></li>
                    </ul>
                </li>
                <li class="menu-item">
                    <a href="javascript:void(0)">
                        <span class="menu-icon">📦</span>
                        <span class="menu-title">物料管理</span>
                        <span class="menu-arrow"></span>
                    </a>
                    <ul class="submenu">
                        <li><a href="material/material_list.html">物料列表</a></li>
                        <li><a href="material/material_edit.html">添加物料</a></li>
                    </ul>
                </li>
                <li class="menu-item">
                    <a href="javascript:void(0)">
                        <span class="menu-icon">⚙️</span>
                        <span class="menu-title">设备管理</span>
                        <span class="menu-arrow"></span>
                    </a>
                    <ul class="submenu">
                        <li><a href="device/device_list.html">设备列表</a></li>
                        <li><a href="device/device_edit.html">添加设备</a></li>
                    </ul>
                </li>
                <li class="menu-item">
                    <a href="javascript:void(0)">
                        <span class="menu-icon">📝</span>
                        <span class="menu-title">产品管理</span>
                        <span class="menu-arrow"></span>
                    </a>
                    <ul class="submenu">
                        <li><a href="product/product_list.html">产品列表</a></li>
                        <li><a href="product/product_edit.html">添加产品</a></li>
                    </ul>
                </li>
                <li class="menu-item">
                    <a href="javascript:void(0)">
                        <span class="menu-icon">🏢</span>
                        <span class="menu-title">供应商管理</span>
                        <span class="menu-arrow"></span>
                    </a>
                    <ul class="submenu">
                        <li><a href="provider/provider_list.html">供应商列表</a></li>
                        <li><a href="provider/provider_edit.html">添加供应商</a></li>
                    </ul>
                </li>
            </ul>
        </aside>

        <!-- 主内容区域 -->
        <main class="admin-main">
            <div class="content-wrapper">
                <div class="content-header">
                    <h1 id="pageTitle">管理员控制台</h1>
                </div>
                <div class="content-body">
                    <div class="welcome-message">
                        <h2>欢迎使用生产过程管理系统</h2>
                        <p>您已登录为系统管理员，拥有系统的全部管理权限。</p>
                    </div>
                </div>
            </div>
        </main>
    </div>
</body>
<script>
    document.addEventListener('DOMContentLoaded', function () {
        var menuItems = document.querySelectorAll('.menu-item > a');
        menuItems.forEach(function (item) {
            item.addEventListener('click', function (e) {
                var parent = item.parentElement;
                document.querySelectorAll('.menu-item').forEach(function (el) {
                    if (el !== parent) el.classList.remove('active');
                });
                parent.classList.toggle('active');
                e.preventDefault();
            });
        });

        // 添加首页链接的点击事件
        document.querySelector('.menu-item:first-child > a').addEventListener('click', function (e) {
            e.preventDefault(); // 阻止默认行为
            window.location.href = 'homepage/administrator_home.html'; // 跳转到管理员首页
        });
    });

    function logout() {
        // 添加确认提示
        if (confirm('确定要退出登录吗？')) {
            // 清除本地存储的登录信息
            localStorage.removeItem('userInfo');
            localStorage.removeItem('token');

            // 跳转到登录页面
            window.location.href = '../../login.html';
        }
    }
</script>

</html>